View Javadoc

1   /*
2    * J.A.D.E. Java(TM) Addition to Default Environment.
3    * Latest release available at http://jade.dautelle.com/
4    * This class is public domain (not copyrighted).
5    */
6   package ch.twiddlefinger.inet.rewinder.model.parser.conversion;
7   
8   
9   /***
10   * <p> This class represents a reentranl lock with the same semantics as
11   *     built-in Java synchronized locks: Once a thread has a lock, it can
12   *     re-obtain it any number of times without blocking. </p>
13   *
14   * <p><i> This class is <b>public domain</b> (not copyrighted).</i></p>
15   *
16   * @author  <a href="http://gee.cs.oswego.edu/dl/">Doug Lea</a>
17   * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
18   * @version 4.4, March 31, 2003
19   */
20  public class ReentrantLock {
21      /***
22   * Holds the owner of this lock.
23   */
24      private Thread _owner;
25  
26      /***
27   * Holds the number of time this lock has been acquired by its owner.
28   */
29      private long _count;
30  
31      /***
32   * Default constructor.
33   */
34      public ReentrantLock() {
35      }
36  
37      /***
38   * Acquires the lock.
39   */
40      public void lock() {
41          Thread caller = Thread.currentThread();
42  
43          synchronized (this) {
44              if (caller == _owner) {
45                  _count++;
46              } else {
47                  try {
48                      while (_owner != null) {
49                          this.wait();
50                      }
51  
52                      _owner = caller;
53                      _count = 1;
54                  } catch (InterruptedException exception) {
55                      return;
56                  }
57              }
58          }
59      }
60  
61      /***
62   * Acquires the lock only if it not held by another thread.
63   *
64   * @return <code>true</code> if the lock was free and was acquired by the
65   *         current thread, or the lock was already held by the current
66   *         thread; <code>false</code> otherwise.
67   */
68      public boolean tryLock() {
69          synchronized (this) {
70              if (_owner == null) {
71                  lock();
72  
73                  return true;
74              } else {
75                  return false;
76              }
77          }
78      }
79  
80      /***
81   * Attempts to release this lock. The lock is actually released if at
82   * least as many {@link #unlock} as {@link #lock} have been performed
83   * on this {@link ReentrantLock} by the current thread.
84   *
85   * throws IllegalMonitorStateExeception if the current thread does not hold
86   *        this lock.
87   */
88      public void unlock() {
89          synchronized (this) {
90              if (Thread.currentThread() == _owner) {
91                  if (--_count == 0) {
92                      _owner = null;
93                      this.notify();
94                  }
95              } else {
96                  throw new IllegalMonitorStateException(
97                      "Current thread does not hold this lock");
98              }
99          }
100     }
101 
102     /***
103  * Returns the thread owner of this {@link ReentrantLock}.
104  *
105  * @return the owner of this lock.
106  */
107     public Thread getOwner() {
108         return _owner;
109     }
110 }